<?php
if(!defined('IN_ADAGPS'))
{
    die(SYSTEM_ACCESS_DENIED);
}

    $objXajax->registerFunction('frmchartstatistic_viewchart'); 

    function frmchartstatistic_viewchart($charttype=1,$dateFrom='',$dateTo=''){
    
        global $objDbSelect;
        $arrTitle = array(
        1=>'Số Lượt thuê theo ngày',        
        2=>'Số lượt trả phòng theo ngày ',
        3=>'Tỉ lệ lượt thuê phòng theo ngày trong tuần',
        4=>'Tỉ lệ lượt trả phòng theo ngày trong tuần',

    );
        $objResponse = new xajaxResponse();
        include(ROOT_PATH.'adagps.com/Charts/ofc/php-ofc-library/open-flash-chart.php');    
        $dateFrom = $dateFrom==''?SYSTEM_DATE_START  : $dateFrom;
        $dateTo = $dateTo==''?SYSTEM_DATE  : $dateTo;
        $arrResult = frmchartstatistic_createArray($charttype,$dateFrom,$dateTo);
        
        $arrName = Array(); 
        $max = 5;
        $titlename = $arrTitle[$charttype];
        $title = new title($titlename,20) ;
        $format = 2;
        $sName='';
        $max =0;
        $min =0;
        $chart = new open_flash_chart();
        $barLabel = array();
        $barArr = array();
        $bar =0;        
        if($charttype==1||$charttype==2){
            for($i=0;$i<count($arrResult);$i++){
                $sName =  $arrResult[$i]['dmy'];
                $xx = new x_axis_label($sName, '#050069', 13, 340);            
                $barLabel[] = $xx;
                $dec = (int)$arrResult[$i]['countnum'];
                if($dec>$max)$max=$dec;
                if($dec<$min)$min=$dec;                             
                $barArr[] = $dec; 
               
              }
        $bar = new bar_glass();
        $bar->set_colour('#68D5ED');
        $bar->set_values($barArr);
        $bar->key($titlename,12);
              
             
        }
        else if($charttype==3||$charttype==4){
            $barArr = array();
            for($i=0;$i<count($arrResult);$i++){
                $sName =  convertToNameDayOfWeek($arrResult[$i]['dayweek']);
               // echo $sName;
               // $sName =  $arrResult[$i]['dayweek'];
                $dec = (int)$arrResult[$i]['countnum']; 
                // array_push($barArr,new pie_value($dec,addslashes("$sName")));
                array_push($barArr,new pie_value($dec,addslashes("$sName")));

            }
            $bar = new pie();  
            $bar->set_alpha(0.8);
            $bar->set_start_angle( 35 );
            $bar->add_animation( new pie_fade() )->add_animation( new pie_bounce(10) ) ; 
            $bar->set_tooltip( '#val# <br> #percent# ' );
            $bar->set_colours(array('#1C9E05','#FF368D','#0000FF','#06DCFB','#FF800D','#B96F6F','#06DCFB'));
            $bar->set_values( $barArr );

        }
        if($charttype==1||$charttype==2){
            $rangeMax =0;
            $rangeMin =0;
            $rangStep=1;
            createRangeForChart($min,$max,$rangeMax,$rangeMin);
            $y = new y_axis();
            $y->set_range($rangeMin,$rangeMax);
            $y->set_steps(($rangeMax-$rangeMin)/5);
            
            $x_labels = new x_axis_labels();
            $x = new x_axis();
            $x_labels->set_labels($barLabel );
            $x->set_labels($x_labels);
               
            $chart->set_x_axis($x);
            $chart->set_y_axis($y);
            
        }
        
        

        $chart->add_element($bar);
        $chart->set_title($title);        
        $values = $chart->toPrettyString();
        
       $sScript = 'data_chartstatistic=' . $values .';';                
        
       $objResponse->addScript($sScript);    
        $sScript1 = 'swfobject.embedSWF("Charts/ofc/open-flash-chart.swf", "frmchartstatistic_divGrid", "60%", "400", "9.0.0", "expressInstall.swf", {"get-data":"open_flash_chart_data_chartstatistic"}, {"wmode":"opaque"});';
        $objResponse->addScript($sScript1);  
        $objResponse->addScript("changeWaiting(1);");  
                          
        return $objResponse->getXML();    
    }
    
function frmchartstatistic_createArray($charttype,$dateFrom,$dateTo){
    global $objDbUpdate,$tbl_postfix;
    
    $contentselect = '';
    $groupby=='';
    switch($charttype){
        case 1:
        case 3:
        $groupby=" group by DATE_FORMAT(timebegin,'%Y-%m-%d') order by  DATE_FORMAT(timebegin,'%Y-%m-%d') ";
        $contentselect = 'timebegin';
        break;
        case 2:
        case 4:
        $contentselect = '';
        $groupby=" group by DATE_FORMAT(timeend,'%Y-%m-%d') order by  DATE_FORMAT(timeend,'%Y-%m-%d') ";
        $contentselect = 'timeend';
        break;       
        default: break;
    }
      
    $sql = " select count(id) as countnum, DAYOFWEEK($contentselect) as dayweek,DATE_FORMAT($contentselect,'%Y-%m-%d') as dmy
     from tbl_history_book_room$tbl_postfix where datediff('$dateFrom',$contentselect)<=0 and datediff('$dateTo',$contentselect)>=0 $groupby ";//
     
     if($charttype==3||$charttype==4){
       $sql = " select AVG(countnum) as countnum,dayweek FROM ( select count(id) as countnum, DAYOFWEEK($contentselect) as dayweek
     from tbl_history_book_room$tbl_postfix where datediff('$dateFrom',$contentselect)<=0 and datediff('$dateTo',$contentselect)>=0 $groupby ) as temp group by dayweek";//  
     }
     $arr = $objDbUpdate->GetArray($sql);
     return  $arr  ;
} 
?>